package com.telit.dispose.entity.dispose;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 元数据-表
 * Create by Chencf 2022/08/05
 */
@Data
@TableName("plat_meta_table")
public class PlatMetaTableEntity implements Serializable {

    public static final String IS_CREATED_YES = "1";
    public static final String IS_CREATED_NO = "0";
    public static final String PRE_FIX = "comm_config_";

    //@MetaData(value = "编号")
    @TableId(value = "id", type = IdType.UUID)
    @Length(max = 32)
    private String id;

    @ApiModelProperty(value = "编码 表的唯一标识(表名  逻辑表)  和prefix搭配组成实际的表名称")
    @TableField("code")
    @NotBlank
    @Length(max = 32)
    private String code;

    @ApiModelProperty(value = "设置菜单名称（在代码里面理解为表名称）（比如：林场专业队驻点）")
    @TableField("name")
    @NotBlank
    @Length(max = 100)
    private String name;
    @ApiModelProperty(value = "和code 搭配组成实际的表名称  比如 prefix_code= 实际表名称")
    @TableField("prefix")
    @Length(max = 20)
    private String prefix = PRE_FIX;

    @ApiModelProperty(value = "分类  ")
    @TableField("category")
    @Length(max = 20)
    private String category;

    @ApiModelProperty(value = "是否创建  0 否 1 是")
    @TableField("is_create")
    @Length(max = 1)
    private String isCreate = IS_CREATED_NO;

    //@MetaData(value = "实体类")
    @TableField("entity")
    @Length(max = 255)
    private String entity;

    @ApiModelProperty(value = "描述")
    @TableField("description")
    @Length(max = 500)
    private String description;

    @ApiModelProperty(value = "类型")
    @TableField("type")
    @Length(max = 50)
    private String type;

    @ApiModelProperty(value = "删除标识  0 否 1 是")
    @TableField("delete_flag")
    @Length(max = 1)
    private String deleteFlag = IS_CREATED_NO;

    @ApiModelProperty(value = "创建人编号")
    @TableField("create_by")
    @Length(max = 32)
    private String createBy;

    @ApiModelProperty(value = "创建人姓名")
    @TableField("create_name")
    @Length(max = 100)
    private String createName;

    @ApiModelProperty(value = "创建日期")
    @TableField("create_date")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date createDate;

    @ApiModelProperty(value = "修改人编号")
    @TableField("update_by")
    @Length(max = 32)
    private String updateBy;

    @ApiModelProperty(value = "修改人姓名")
    @TableField("update_name")
    @Length(max = 100)
    private String updateName;

    @ApiModelProperty(value = "修改日期")
    @TableField("update_date")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date updateDate;

    //@MetaData(value = "租户")
    @TableField("tentment_id")
    @Length(max = 100)
    private String tentmentId;

    @ApiModelProperty(value = "图标是否显示 0 否 1是 默认 1  0 否 1 是")
    @TableField("icon_show")
    private String iconShow = IS_CREATED_YES;

    @ApiModelProperty(value = "icon   url")
    @TableField("img")
    private String img;


    //@MetaData(value = "字段集")
    @TableField(exist = false)
    private List<PlatMetaFieldEntity> fields = new ArrayList<>();

    /**
     * 设置编码
     *
     * @param code 编码
     */
    public void setCode(String code) {
        if (StringUtils.isNotBlank(code)) {
            this.code = code.toLowerCase();
        } else {
            this.code = null;
        }
    }

    public String setTableName(String code, String prefix) {
        if (StringUtils.isNotBlank(code) && StringUtils.isNotBlank(prefix)) {
            return prefix + code;
        } else {
            return null;
        }
    }

    /**
     * 设置表id
     *
     * @param tableId 表id
     */
    public void updateFieldTableId(String tableId) {
        for (PlatMetaFieldEntity field : fields) {
            field.setTableId(tableId);
        }
    }

}
